package com.graduate.examapp.view.fragment

import android.content.Context
import android.net.ConnectivityManager
import android.net.NetworkInfo
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.floatingactionbutton.FloatingActionButton
import com.graduate.examapp.R
import com.graduate.examapp.adapter.ExamTaskAdapter
import com.graduate.examapp.databinding.FragmentRangeBinding
import com.graduate.examapp.entity.UserInfoFactory
import com.graduate.examapp.view.customview.CustomFormDialog
import com.graduate.examapp.viewmodel.RangeViewModel

class RangeFragment : Fragment() {

    private val rangeViewModel: RangeViewModel by lazy {
        val model = ViewModelProvider(this)[RangeViewModel::class.java]
        model
    }

    private var binding: FragmentRangeBinding? = null
    private var addFab: FloatingActionButton? = null

    private var examTaskRecyclerView: RecyclerView? = null

    private val cancelCallBack = { id: Int, position: Int ->
        Log.d("RangeFragment", id.toString())
        rangeViewModel.cancelExam(id, position)
    }

    //private var firstDate: LocalDate? = null
    //private var setStartDateBtn: FloatingActionButton? = null
    private val examTaskAdapter = ExamTaskAdapter(cancelCallBack)
    //在创建视图的时候 发起请求 节约资源
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        Log.i("HomeFragment", "create")
        binding = DataBindingUtil.inflate<FragmentRangeBinding>(
            inflater,
            R.layout.fragment_range,
            container,
            false
        )
        //用户基础信息
        init()
        observe()
        return binding?.root
    }

    override fun onResume() {
        super.onResume()
        request()
    }

    private fun init() {
        addFab = binding?.root?.findViewById(R.id.floating_addexam_btn)

        addFab?.setOnClickListener {
            CustomFormDialog(requireContext()) { examRangeParam ->
                val connectivityManager = this.context?.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
                val info: NetworkInfo? = connectivityManager.getActiveNetworkInfo()
                if (info != null && info.isAvailable()) {
                    rangeViewModel.rangeExam(examRangeParam)
                } else {
                    Toast.makeText(this.context, "网络连接不可用", Toast.LENGTH_LONG).show()
                }
            }.show()
        }
        examTaskRecyclerView = binding?.root?.findViewById(R.id.examtask_recycle)
        examTaskRecyclerView?.layoutManager = LinearLayoutManager(this.context, LinearLayoutManager.VERTICAL, false)

        examTaskRecyclerView?.adapter = examTaskAdapter
    }

    private fun observe() {
        rangeViewModel.rangeSuccessLiveData.observe(requireActivity()) {
            Toast.makeText(context, it, Toast.LENGTH_SHORT).show()
        }

        rangeViewModel.rangeLiveData.observe(requireActivity()) {
            it?.let {
                examTaskAdapter.dataSource = it
                examTaskAdapter.notifyDataSetChanged()
            }
        }
    }

    //请求获取所有监考任务
    private fun request() {
        Log.d("RangeFragment", UserInfoFactory.loginData?.insName.toString())
        UserInfoFactory.loginData?.insName?.let {
            val connectivityManager = this.context?.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
            val info: NetworkInfo? = connectivityManager.getActiveNetworkInfo()
            if (info != null && info.isAvailable()) {
                rangeViewModel.fetchAllExamTask(it)
            } else {
                Toast.makeText(this.context, "网络连接不可用", Toast.LENGTH_LONG).show()
            }
        }
    }
}